//
//  SRHandRingView.m
//  SportRing
//
//  Created by icelee on 14-11-1.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRHandRingView.h"

#import "SRBoundHandRingTableViewCell.h"
#import "SRRemoveHandRingTableViewCell.h"

@interface SRHandRingView()<SRRemoveHandRingTableViewCellDelegate,SRBoundHandRingTableViewCellDelegate>

@property (nonatomic, weak)IBOutlet UITableView *srHandRingTableView;

@end

@implementation SRHandRingView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self.srHandRingTableView setBackgroundColor:[UIColor colorWithRed:246.0/255.0 green:245.0/255.0 blue:240.0/255.0 alpha:1.0]];
}

- (void)setCanUseHandRingArray:(NSMutableArray *)canUseHandRingArray
{
    if (_canUseHandRingArray == canUseHandRingArray) {
        return;
    }
    _canUseHandRingArray = canUseHandRingArray;
    
    [_srHandRingTableView reloadData];
}

- (void)setBoundedHandRingArray:(NSMutableArray *)boundedHandRingArray
{
    if (_boundedHandRingArray == boundedHandRingArray) {
        return;
    }
    _boundedHandRingArray = boundedHandRingArray;
    
    [_srHandRingTableView reloadData];
    
}

#pragma mark - UITableViewDataSource ,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    if (section == 0)
    {
        return _boundedHandRingArray ? [_boundedHandRingArray count]:1;
    }
    else
    {
        return _canUseHandRingArray ? [_canUseHandRingArray count]:2;
    }
    
    return 0;
    
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{
    UIView *footerView = nil;
    
    if (section == 0)
    {
        footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.srHandRingTableView.frame.size.width, 10)];
    }
    else
    {
        footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.srHandRingTableView.frame.size.width, 0)];
    }
    
    footerView.backgroundColor=[UIColor clearColor];
    
    return footerView;
    
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    UIView *headerView = nil;
    
    if (section == 0)
    {
        headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.srHandRingTableView.frame.size.width, 50)];
        headerView.backgroundColor=[UIColor clearColor];
        return headerView;
        
    }
    
    if(section == 1)
    {
        headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.srHandRingTableView.frame.size.width, 50)];
        headerView.backgroundColor=[UIColor clearColor];
        UILabel *headerTitleView = [[UILabel alloc] initWithFrame:CGRectMake(25, 0, headerView.frame.size.width, headerView.frame.size.height)];
        [headerTitleView setText:@"绑定手环"];
        headerTitleView.font = [UIFont systemFontOfSize:12];
        [headerTitleView setTextColor:[UIColor darkGrayColor]];
        [headerView addSubview:headerTitleView];
        return headerView;
    }
    
    return headerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 10;
    }
    return 0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 50;
    }
    else if(section == 1)
    {
        return 50;
        
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SRBoundHandRingTableViewCell *boundCell = [tableView dequeueReusableCellWithIdentifier:@"SRBoundHandRingTableViewCell"];
    SRRemoveHandRingTableViewCell *removeCell = [tableView dequeueReusableCellWithIdentifier:@"SRRemoveHandRingTableViewCell"];
    
    NSString * removeRing = nil;
    if ([_boundedHandRingArray count] > 0)
    {
        removeRing= [_boundedHandRingArray objectAtIndex:0];

    }
    NSString *boundRing = nil;
    if ([_canUseHandRingArray count] > 0)
    {
         boundRing= [_canUseHandRingArray objectAtIndex:indexPath.row];
    }
    
    if (indexPath.section == 0)
    {
        [removeCell setRemoveHandRing:removeRing];
        return removeCell;
    }
    else if(indexPath.section == 1)
    {
        [boundCell setBoundHandRing:boundRing];
        return boundCell;
        
    }
    else
    {
        return nil;
        
    }
}

#pragma mark - SRBoundHandRingTableViewCellDelegate

- (void)boundHandRing:(NSString *)ringID
{
    if (_srHandRingDelegate && [_srHandRingDelegate respondsToSelector:@selector(boundHandRing:)])
    {
        [_srHandRingDelegate boundHandRing:ringID];
    }
    
}

#pragma mark - SRRemoveHandRingTableViewCellDelegate

- (void)removeHandRing:(NSString *)ringID;
{
    if (_srHandRingDelegate && [_srHandRingDelegate respondsToSelector:@selector(removeHandRing:)])
    {
        [_srHandRingDelegate removeHandRing:ringID];
    }
    
}



@end
